﻿using RestSharp;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web;
using TheAPI.Data;

namespace TheAPI
{
    public class SummonerModule : Module
    {
        private const string VERSION = "v1.3";
        private const string MODULE = "summoner";

        public SummonerModule(RiotGamesAPI api)
            : base(api)
        {
        }

        private RestRequest BuildRequest(List<string> summonerIds, string requested)
        {
            var request = new RestRequest("api/lol/{region}/{version}/{module}/{summonerIds}/" + requested, Method.GET);
            request.AddUrlSegment("region", API.RegionString);
            request.AddUrlSegment("version", VERSION);
            request.AddUrlSegment("module", MODULE);
            request.AddUrlSegment("summonerIds", HttpUtility.UrlEncode(String.Join(",", summonerIds)));

            request.AddParameter("api_key", API.APIKey);

            return request;
        }

        public MasteryPages RetrieveMasteries(string summonerId)
        {
            return RetrieveMasteries(new List<string>() { summonerId })[summonerId];
        }

        public Dictionary<string, MasteryPages> RetrieveMasteries(List<string> summonerIds)
        {
            if (summonerIds.Count > 40)
                throw new ArgumentOutOfRangeException("Maximum allowed summonerIds at once is 40.");

            var request = BuildRequest(summonerIds, "masteries");

            return REST<Dictionary<string, MasteryPages>>(request);
        }

        public RunePages RetrieveRunes(string summonerId)
        {
            return RetrieveRunes(new List<string>() { summonerId })[summonerId];
        }

        public Dictionary<string, RunePages> RetrieveRunes(List<string> summonerIds)
        {
            if (summonerIds.Count > 40)
                throw new ArgumentOutOfRangeException("Maximum allowed summonerIds at once is 40.");

            var request = BuildRequest(summonerIds, "runes");

            return REST<Dictionary<string, RunePages>>(request);
        }

        public Summoner RetrieveSummonerByName(string summonerName)
        {
            try
            {
                return RetrieveSummonerByName(new List<string>() { summonerName })[summonerName.ToLower()];
            }
            catch
            {
                return null;
            }
        }

        public Dictionary<string, Summoner> RetrieveSummonerByName(List<string> summonerNames)
        {
            if (summonerNames.Count > 40)
                throw new ArgumentOutOfRangeException("Maximum allowed summonerNames at once is 40.");

            var request = new RestRequest("api/lol/{region}/{version}/{module}/by-name/{summonerNames}", Method.GET);
            request.AddUrlSegment("region", API.RegionString);
            request.AddUrlSegment("version", VERSION);
            request.AddUrlSegment("module", MODULE);
            request.AddUrlSegment("summonerNames", String.Join(",", summonerNames));

            request.AddParameter("api_key", API.APIKey);

            return REST<Dictionary<string, Summoner>>(request);
        }

        public string RetrieveSummonerName(string summonerId)
        {
            return RetrieveSummonerNames(new List<string>() { summonerId })[summonerId];
        }

        public Dictionary<string, string> RetrieveSummonerNames(List<string> summonerIds)
        {
            if (summonerIds.Count > 40)
                throw new ArgumentOutOfRangeException("Maximum allowed summonerIds at once is 40.");

            var request = BuildRequest(summonerIds, "name");

            return REST<Dictionary<string, string>>(request);
        }

        public Summoner RetrieveSummoner(string summonerId)
        {
            return RetrieveSummoner(new List<string>() { summonerId })[summonerId];
        }

        public Dictionary<string, Summoner> RetrieveSummoner(List<string> summonerIds)
        {
            if (summonerIds.Count > 40)
                throw new ArgumentOutOfRangeException("Maximum allowed summonerIds at once is 40.");

            var request = new RestRequest("api/lol/{region}/{version}/{module}/{summonerIds}", Method.GET);
            request.AddUrlSegment("region", API.RegionString);
            request.AddUrlSegment("version", VERSION);
            request.AddUrlSegment("module", MODULE);
            request.AddUrlSegment("summonerIds", String.Join(",", summonerIds));

            request.AddParameter("api_key", API.APIKey);

            return REST<Dictionary<string, Summoner>>(request);
        }
    }
}
